package bandi.osgi.dal;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import bandi.osgi.dal.dao.devices.DeviceDAO;
import bandi.osgi.dal.dao.devices.impl.DeviceDAOImpl;
import bandi.osgi.dal.dao.devicetypes.DeviceTypeDAO;
import bandi.osgi.dal.dao.devicetypes.impl.DeviceTypeDAOImpl;
import bandi.osgi.dal.dao.users.UsersDAO;
import bandi.osgi.dal.dao.users.impl.UsersDAOImpl;

public class DALActivator implements BundleActivator {
	private ServiceRegistration myServiceRegistration;
	private UsersDAOImpl udi;
	private DeviceDAOImpl ddi;
	private DeviceTypeDAOImpl dtdi;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		String name = context.getBundle().getSymbolicName();
		// DAO service registrations
		System.out.println("\tBundle " + name + " starting:");
		System.out.println("\t\tRegistering service objects..");
		try {
			udi = new UsersDAOImpl();
			ddi = new DeviceDAOImpl();
			dtdi = new DeviceTypeDAOImpl();
			myServiceRegistration = context.registerService(UsersDAO.class
					.getName(), udi, null);
			myServiceRegistration = context.registerService(DeviceDAO.class
					.getName(), ddi, null);
			myServiceRegistration = context.registerService(DeviceTypeDAO.class
					.getName(), dtdi, null);
			System.out.println("\t\tService objects registered.\n\tBundle "
					+ name + " is active.");
		} catch (Exception e) {
			System.out.println("ERROR(" + name
					+ "): could not register service objects! Message: "
					+ e.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		String name = context.getBundle().getSymbolicName();
		myServiceRegistration.unregister();
		System.out.println("\tBundle " + name
				+ " has unregistered its services.");
	}

}
